/** @format */

import { USER_CONF } from '../contants/configCont'
import { navUrl } from './navAdapter'

const BASE_URL = 'https://gitee.com/api/v5/repos/'

export function extractTitleFromPath(path: string): string {
  return decodeURIComponent(path.split('/').pop()?.replace('.md', '') || '')
}

export function buildGiteeUrl(
  owner: string,
  repo: string,
  path: string
): string {
  return `${BASE_URL}${owner}/${repo}/contents/${path}`
}

export function buildDefaultUrl(path) {
  return buildGiteeUrl(USER_CONF.owner, USER_CONF.repo, path)
}

export function parseGiteeUrl(url: string) {
  // 解析出 owner、repo、path
  const [owner, repo, , ...rest] = url.replace(BASE_URL, '').split('/')
  return { owner, repo, path: rest.join('/') }
}

export function isSameBook(book1, book2) {
  if (!book1 || !book2) return false
  return book1.owner === book2.owner && book1.repo === book2.repo
}

export function min(value, min) {
  if (value < min) return min
  return value
}

// 节流
export function throttle(fn, delay) {
  let timer: any = null
  return function (...args) {
    if (timer) return
    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}

// 防抖
export function debounce(fn, delay = 1500) {
  let timer: any = null
  return function (...args) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

export function getCurrentPage() {
  const pages = getCurrentPages()
  const currentPage: any = pages[pages.length - 1]
  if (navUrl.value) {
    return navUrl.value
  }
  if (currentPage.$page) {
    return currentPage.$page.fullPath
  }
  let currentPath = '/' + currentPage?.route
  return currentPath
}

// 格式化日期
export function formatDate(timestamp, format = 'yyyy-MM-dd HH:mm:ss') {
  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hours = date.getHours().toString().padStart(2, '0')
  const minutes = date.getMinutes().toString().padStart(2, '0')
  const seconds = date.getSeconds().toString().padStart(2, '0')
  return format
    .replace('yyyy', year.toString())
    .replace('MM', month)
    .replace('dd', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

export function getDateYMD(timestamp) {
  return formatDate(timestamp, 'yyyy-MM-dd')
}

export function formatDateYMDHMS(timestamp) {
  return formatDate(timestamp, 'yyyy-MM-dd HH:mm:ss')
}

// 防按钮连点操作立即触发
export function preventRepeatedClick(fn, delay = 800) {
  let timer: any = null
  return function (...args) {
    if (timer) return
    timer = setTimeout(() => {
      timer = null
    }, delay)
    fn.apply(this, args)
  }
}

// 取得下一个月
export function getNextMonth(d) {
  const date = new Date(d)
  const year = date.getFullYear()
  const month = date.getMonth()
  const nextMonth = new Date(year, month + 2, 1)
  return nextMonth.getTime()
}

// 降序排序
export function sortByDesc(list) {
  return list.sort((a, b) => b - a)
}
